var of = require('OverFish'),
    couchdb = require('couchdb'),
    User = require('../models/User').User,
    sys = require('sys');

exports.all = function(cb) {
    var db = couchdb.createClient(of.env.settings['db_port'], of.env.settings['db_host']).db('overfish');
    db.view('overfish', 'all', {}, function(err, res) {
        if(!err) {
            var users = []
            res.rows.forEach(function(row) {
                var user = new User();
                //sys.puts(sys.inspect(user));
                user.username(row.value.username)
                    user.password(row.value.password)
                    user.name(row.value.name)
                    user.email(row.value.email);
                users.push(user);
            });
        }
        cb(err, users);
    });
};

exports.save = function(user, cb) {
    var doc = {
        username: user.username(),
        name: user.name(),
        password: user.passwordHash(),
        surname: user.surname(),
        email: user.email(),
        roles: ['aa', 'bb', 'cc']
    };
    var db = couchdb.createClient(of.env.settings['db_port'], of.env.settings['db_host']).db('overfish');
    db.saveDoc(doc, function(err) {
        cb(err);
    });
};